// pages/housing-resources/detail/index.js
// 房源详情
import api from '../../../api/index';
// var amapFile = require('../../../libs/amap-wx'); //引入高德js
// var config = require('../../../libs/config.js'); //引用我们的配置文件
Page({

  /**
   * 页面的初始数据
   */
  data: {
    duration: 500,
    list: [],
    currentIndex: 1,
    id: '',
    detail: {}, //详情
    markers: [],
    latitude: '',
    longitude: '',
    textData: {},
    show: false, //是否显示弹出层
    sharwShow: false, //分享海报弹出层
    formData: {
      basicBorrowerName: "",
      basicBorrowerPhone: "",
      cid: '',
      productId: '',
      generalizeId:'',  //邀请人id
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  id: '', //扫码进入的id
  inviterId: '', //邀请人id
  logInfo:{},//登录信息
  onLoad: function (options) {
    this.logInfo=wx.getStorageSync('logInfo') || {};
    this.inviterId=options.inviterId || '';
    if (options.scene) {
      let scene = decodeURIComponent(options.scene).split('&');
      this.id = scene[0]; //扫码详情id
      this.inviterId = scene[1]; //邀请人id
    }
    
    this.setData({
      id: options.id || this.id
    })
    if (this.data.id) {
      this.getDetail();
    }

  },
  //取消分享
  cancel() {
    this.setData({
      shareBtn: false
    })
  },
  //去海报页
  goPosters() {
    wx.navigateTo({
      url: `/pages/detail-poster-shaer/index?detail=${JSON.stringify(this.data.detail)}`,
    })
  },
  //显示分享弹出层
  shareBtn() {
    this.setData({
      shareBtn: true
    })
  },
  //显示弹层
  showLayer() {
    this.setData({
      show: true
    })
  },
  //关闭弹层
  close() {
    this.setData({
      show: false
    })
  },
  //提交
  formSubmit(e) {
    let data = e.detail.value;
    this.setData({
      [`formData.basicBorrowerName`]: data.basicBorrowerName,
      [`formData.basicBorrowerPhone`]: data.basicBorrowerPhone,
      [`formData.cid`]: this.data.detail.id,
      [`formData.productId`]: this.data.detail.productId,
      [`formData.generalizeId`]: this.inviterId,
    })
    api.orderSave(this.data.formData).then(res => {
      this.close();
      this.setData({
        [`detail.isApply`]: true
      })
      wx.showToast({
        title: '提交成功',
      })
    })
  },
  //申请
  apply() {
    if (!this.data.detail.isApply) {
      this.showLayer();
    } else {
      wx.showToast({
        icon: 'none',
        title: '已申请过',
      })
    }
  },
 
  //获取房源详情
  getDetail() {
    api.getHousingDetail(this.data.id).then(res => {
      let rt = res.data.data;
      rt.tbHouse = rt.tbHouse ? rt.tbHouse : {};
      rt.tbHouse.trait = rt.tbHouse.trait ? rt.tbHouse.trait.split(/[\n\s+；;]/g) : [];
      rt.tbHouse.merit = rt.tbHouse.merit ? rt.tbHouse.merit.split(/[\n\s+；;]/g) : [];
      // console.log("...",data)
      // console.log(rt)
      this.setData({
        detail: rt,
        markers: [{
          id: 1,
          latitude: rt.tbHouse.la,
          longitude: rt.tbHouse.lo,
        }]
      })
    })
  },
  // 切换swiper
  onChange(e) {
    // console.log(e);
    this.setData({
      currentIndex: e.detail.current
    })
  },

  onShareTimeline: function () {
    return {
      title: this.data.detail.title,
      query: `/pages/housing-resources/detail/index?id=${this.data.id}`,
      imageUrl: this.data.detail.wxCoverImg,
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.detail.title,
      query: `/pages/housing-resources/detail/index?id=${this.data.id}&inviterId=${this.logInfo.id}`,
      imageUrl: this.data.detail.wxCoverImg,
    }
  }
})